package com.one.config;

import com.one.constant.RabbitMQConstant;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Create on 2022-01-17
 * <p>
 * Author: LiangWei
 * <p>
 * Description: 发送主题消息配置类
 **/
@Configuration
public class RabbitMQTopicConfig {
    @Bean
    public Queue topicQueueOne(){
        return new Queue(RabbitMQConstant.RABBITMQ_TOPIC_QUEUE_ORDER_ONE,true);
    }

    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(RabbitMQConstant.RABBITMQ_TOPIC_EXCHANGE);
    }

    @Bean
    public Binding bingingTopicOne(){
        return BindingBuilder.bind(topicQueueOne()).to(topicExchange()).with(RabbitMQConstant.RABBITMQ_TOPIC_ROUTING_BATCH);
    }

    @Bean
    public Queue delayQueue(){
        // 创建队列需要的参数
        Map<String, Object> args = new HashMap<>();
         //设置TTL
        args.put("x-message-ttl", 60000);
//        args.put("x-expires", 1800000);
        // 设置死信的目的交换机
        args.put("x-dead-letter-exchange", RabbitMQConstant.RABBITMQ_DIRECT_DLX_EXCHANGE);
        // 设置死信交给目的交换机时的路由键
        args.put("x-dead-letter-routing-key", RabbitMQConstant.RABBITMQ_DIRECT_DLX_KEY);
        return new Queue(RabbitMQConstant.RABBITMQ_DIRECT_QUEUE,true,false,false,args);
    }
    @Bean
    public DirectExchange exchangeDirect(){
        return new DirectExchange(RabbitMQConstant.RABBITMQ_DIRECT_EXCHANGE,true,false,null);
    }
    @Bean
    public Binding bingingDirect(){
        return BindingBuilder.bind(delayQueue()).to(exchangeDirect()).with(RabbitMQConstant.RABBITMQ_DIRECT_KEY);
    }

    @Bean
    public Queue queueDlx(){
        return new Queue(RabbitMQConstant.RABBITMQ_DIRECT_DLX_QUEUE,true,false,false);
    }
    @Bean
    public DirectExchange exchangeDlx(){
        return new DirectExchange(RabbitMQConstant.RABBITMQ_DIRECT_DLX_EXCHANGE,true,false,null);
    }
    @Bean
    public Binding bingingDlx(){
        return BindingBuilder.bind(queueDlx()).to(exchangeDlx()).with(RabbitMQConstant.RABBITMQ_DIRECT_DLX_KEY);
    }

}

